﻿/*------------------------------------------------------------------------

TXTSTORAGE.HPP v3.4

Copyright 楊志賢 CxxlMan, 2011 - 2012
All Rights Reserved

------------------------------------------------------------------------*/
#if !defined(__TXTSTORAGE_HPP_CxxlMan)
#define __TXTSTORAGE_HPP_CxxlMan

#include "PRESERVE2.HPP"
#include "CXXLCOMPOSITE.HPP"
#include "SMART_STRING.HPP"

namespace CxxlMan 
{

class TxtStorage;

extern CXXLPRESERVE_DLLEXPORT void cxxlFASTCALL 
TxtStorageExportTLC(std::wostream &wout, const Smart_Ptr<TxtStorage> &TxtStorage_Ptr);

extern CXXLPRESERVE_DLLEXPORT Smart_Ptr<TxtStorage> cxxlFASTCALL 
TxtStorageImportTLC(std::wistream &win, ISpirit *spirit = Spirit_Easy);

extern CXXLPRESERVE_DLLEXPORT void cxxlFASTCALL
TxtStorageExportXML(std::wostream &wout, const Smart_Ptr<TxtStorage> &TxtStorage_Ptr);

extern CXXLPRESERVE_DLLEXPORT Smart_Ptr<TxtStorage> cxxlFASTCALL 
TxtStorageImportXML(std::wistream &win, ISpirit *spirit = Spirit_Easy);


/*
** 儲存體類別
*/
class CXXLPRESERVE_DLLEXPORT TxtStorage:virtual public IcxxlStorage
{

  // 實際用來存放永續資料的容器
  Smart_Ptr<cxxlComposite<string_wrapper<wchar_t>,false> > Txt_Composite_Ptr;

  virtual bool cxxlFASTCALL toSave(cxxlPreserve *v, const UNICODE_String &Name);
  virtual bool cxxlFASTCALL toLoad(cxxlPreserve *v, const UNICODE_String &Name);

  virtual Smart_Ptr<IPreserve2Storage> cxxlFASTCALL GetIPreserve2Storage(const UNICODE_String &Name);

protected:

public:
  // Constructor
  TxtStorage(ISpirit *spirit = Spirit_Easy);

  virtual ~TxtStorage(){} // Destruciro

  // 成功回報 true
  // 失敗回報 false
  // 失敗原因可能是 Name 已存在或 v 為 NULL，原 Name 中的資料不會被變動。
  template<typename T> bool cxxlFASTCALL Save(const Smart_Ptr<T> &v, const UNICODE_String &Name)
  {
    T *V = v;
    return toSave(V, Name);
  }

  // 成功回報 true
  // 失敗回報 false
  // 失敗原因可能是 Name 不存在或 v 為 NULL，或是存取順序不一致，或物件(或子孫物件)已
  // 被更換過而無和永存資料對應
  template<typename T> bool cxxlFASTCALL Load(const Smart_Ptr<T> &v, const UNICODE_String &Name)
  {
    T *V = v;
    return toLoad(V, Name);
  }

  // 取得永續資料的儲存容器
  Smart_Ptr<cxxlComposite<string_wrapper<wchar_t>,false> > cxxlFASTCALL GetComposite()
  {
    return Txt_Composite_Ptr;
  }


  // 刪除指定名稱的永續儲存資料
  virtual bool cxxlFASTCALL Delete(const UNICODE_String &Name);


  friend CXXLPRESERVE_DLLEXPORT void cxxlFASTCALL 
    TxtStorageExportTLC(std::wostream &wout, const Smart_Ptr<TxtStorage> &TxtStorage_Ptr);
  
  friend CXXLPRESERVE_DLLEXPORT Smart_Ptr<TxtStorage> cxxlFASTCALL 
    TxtStorageImportTLC(std::wistream &win, ISpirit *spirit);

  friend CXXLPRESERVE_DLLEXPORT void cxxlFASTCALL
    TxtStorageExportXML(std::wostream &wout, const Smart_Ptr<TxtStorage> &TxtStorage_Ptr);

  friend CXXLPRESERVE_DLLEXPORT Smart_Ptr<TxtStorage> cxxlFASTCALL 
    TxtStorageImportXML(std::wistream &win, ISpirit *spirit);

};



}   /* namespace CxxlMan */
#endif



